package com.javajavadog.ui.base;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.javajavadog.ui.R;

public abstract class FragmentHostActivity extends Activity {
    private int mFragmentHostId;
    private int mBackButtonId;
    private int mTitleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View rootView = createRootView();

        setContentView(rootView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        initBackTitle(getTitleText());

        initFragment();
    }

    public void initBackTitle(String title) {
        if (enableBackButton()) {
            ImageButton back = (ImageButton) findViewById(mBackButtonId);
            if (back == null) {
                // no back title bar found.
                return;
            } else {
                back.setVisibility(View.VISIBLE);
                back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
            }
        } else {
            ImageButton back = (ImageButton) findViewById(mBackButtonId);
            back.setVisibility(View.GONE);
        }

        TextView t = (TextView) findViewById(mTitleId);
        if (t == null) {
            // no title text found.
            return;
        } else {
            t.setText(title);
        }
    }

    protected void initFragment() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(mFragmentHostId, getContentFragment());
        transaction.commit();
    }

    private View createRootView() {
        LinearLayout root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);

        LinearLayout titleBar = new LinearLayout(this);
        titleBar.setPadding(0, getTitlePaddingTop(), 0, 0);
        titleBar.setBackgroundColor(getTitleBarColor());
        titleBar.setElevation(getElevationPixel());
        ImageButton backButton = new ImageButton(this);
        mBackButtonId = View.generateViewId();
        backButton.setId(mBackButtonId);
        backButton.setImageResource(getBackButtonSrc());
        backButton.setBackground(null);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        TextView title = new TextView(this);
        mTitleId = View.generateViewId();
        title.setId(mTitleId);
        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getTitleBarTextSize());
        title.setTextColor(getTitleBarTextColor());
        title.setText(getTitleText());
        int padding = getPaddingPixel();
        title.setPadding(padding, padding, padding, padding);
        titleBar.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams backButtonLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        backButtonLp.setMarginEnd(-1 * getPaddingPixel());
        titleBar.addView(backButton, backButtonLp);
        titleBar.addView(title, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

        FrameLayout fragmentHost = new FrameLayout(this);
        mFragmentHostId = View.generateViewId();
        fragmentHost.setId(mFragmentHostId);

        root.addView(titleBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        root.addView(fragmentHost, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        return root;
    }

    private float getTitleBarTextSize() {
        return 18;
    }

    public int getTitleBarTextColor() {
        return getResources().getColor(R.color.colorWhite01);
    }

    public int getBackButtonSrc() {
        return R.drawable.btn_back_white;
    }

    public int getTitleBarColor() {
        return getResources().getColor(R.color.colorPrimary);
    }

    public int getTitlePaddingTop() {
        int p = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
        return p;
    }

    public int getPaddingPixel() {
        int p = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
        return p;
    }

    public int getElevationPixel() {
        int p = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
        return p;
    }

    public abstract String getTitleText();

    public abstract Fragment getContentFragment();

    public abstract boolean enableBackButton();
}
